package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-20 16:53
 */
@Slf4j
@RestController
@RequestMapping("/mq")
@RequiredArgsConstructor
public class MqController {

	private final RabbitService rabbitService;

	private final RabbitTemplate rabbitTemplate;

	/**
	 * 发送消息
	 */
	@GetMapping("/sendConfirm")
	public Result sendConfirm() {
		rabbitService.sendMessage("exchange.confirm", "routing.confirm", "fxzcloud...");
		return Result.ok();
	}

	/**
	 * 发送死信消息
	 */
	@GetMapping("/sendDeadLettle")
	public Result sendDeadLettle() {
		log.info("发送死信消息:{}", LocalDateTime.now());
		rabbitService.sendMessage(DeadLetterMqConfig.exchange_dead, DeadLetterMqConfig.routing_dead_1, "10秒过期时间...");
		return Result.ok();
	}

	@GetMapping("/sendDelay")
	public Result sendDelay() {
		log.info("发送延时消息:{}", LocalDateTime.now());
		rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, "10秒过期时间...",
				message -> {
					message.getMessageProperties().setDelay(10 * 1000);
					return message;
				});
		return Result.ok();
	}

}
